package com.ktjiaoyu;

import com.ktjiaoyu.entity.User;
import com.ktjiaoyu.repository.UserRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserRepositoryTester {
    @Resource
    UserRepository userRepository;

    @Test
    public void test() {
        List<User> list = userRepository.findAll();
        for (User user : list) {
            System.out.println(user);
        }

    }

    @Test
    public void test2() {
        User user = new User("laotou", "123456", 1l, 1);
        userRepository.save(user);
    }

    @Test
    public void test3() {
        User user = new User(21l, "laotou2", "1234567", 2l, 0);
        userRepository.save(user);
    }

    @Test
    public void test4() {
        userRepository.deleteById(21l);
    }

    @Test
    public void test5() {
        User user = userRepository.findById(19l).get();
        System.out.println(user);
    }

    @Test
    public void test6() {
        //构造分页条件
        PageRequest pageRequest = PageRequest.of(0, 2);

        Page<User> page = userRepository.findAll(pageRequest);
        System.out.println("当前第几页：" + (page.getNumber() + 1));
        System.out.println("每页显示多少条：" + page.getSize());
        System.out.println("总共多少条：" + page.getTotalElements());
        System.out.println("总共多少页：" + page.getTotalPages());
        System.out.println("每页显示的数据：" + page.getContent());
    }

    @Test
    public void test7() {
        List<User> list = userRepository.findUserByRoleId(1l);

        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void test8() {
        List<User> list = userRepository.findByUsrName("lisi");
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void test9() {
        List<User> list = userRepository.findByUsrNameLike("%l%");
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void test10() {
        List<User> list =
                userRepository.findByUsrNameAndUsrPassword("lisi", "123456");
        for (User user : list) {
            System.out.println(user);
        }
    }

    //等值查询的动态sql
    @Test
    public void test11() {
        String name = "laotou";
        String password = "123456";
        //构造动态sql查询按条件
        Specification<User> specification = new Specification<User>() {

            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery,
                                         CriteriaBuilder criteriaBuilder) {
                //1.获取要查询的属性
                Path usrName = root.get("usrName");
                Path usrPassword = root.get("usrPassword");
                //2.构造条件
                List<Predicate> list = new ArrayList<>();
                //等值查询
//                if (name != null && !"".equals(name)) {
//                    Predicate p = criteriaBuilder.equal(usrName, name);
//                    list.add(p);
//                }
                //模糊查询
//                if (name != null && !"".equals(name)) {
//                    Predicate p = criteriaBuilder.like(usrName, "%" + name + "%");
//                    list.add(p);
//                }

                //多条件
                if (name != null && !"".equals(name)) {
                    Predicate p = criteriaBuilder.equal(usrName, name);
                    list.add(p);
                }
                if (password != null && !"".equals(password)) {
                    Predicate p = criteriaBuilder.equal(usrPassword, password);
                    list.add(p);

                }

                Predicate p = criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
                return p;
            }
        };
        Sort sort = new Sort(Sort.Direction.DESC, "usrId");
        PageRequest pageRequest = PageRequest.of(0, 2, sort);

//        List<User> list = userRepository.findAll(specification);
        Page<User> page = userRepository.findAll(specification, pageRequest);

//        for (User user : list) {
//            System.out.println(user);
//        }
        System.out.println("当前第几页：" + (page.getNumber() + 1));
        System.out.println("每页显示多少条：" + page.getSize());
        System.out.println("总共多少条：" + page.getTotalElements());
        System.out.println("总共多少页：" + page.getTotalPages());
        System.out.println("每页显示的数据：" + page.getContent());
    }
}
